import QtQuick 2.13
import QtQuick.Controls 2.5


Rectangle {

    color: "transparent"
    width: 390
    height: 874

    //    property alias curTemp: tNum.text
    property var ys: []
    property var displayNum: 220

    property bool isIndicatorPressed: false

    //    Text {
    //        id: tNum
    //        text: "220°c"
    //        font.pixelSize: 220
    //        font.family: "UNDA Circle"
    //        anchors.top: parent.top
    //        anchors.left: parent.left
    //        color: "white"
    //    }

    Image {
        id: tNum
        width: 379  //372
        height: 135 //180
        source: "qrc:/imgs/themoCount/220.png"
        anchors.top: parent.top
        //        anchors.topMargin: 70
        anchors.left: parent.left
    }

    Image {
        id: meterBody
        //        anchors.bottom: parent.bottom
        anchors.top: tNum.bottom
        anchors.topMargin: 115
        anchors.left: parent.left
        anchors.leftMargin: 146
        source: "qrc:/imgs/toastmode/thermometer.png"
    }



    Image {
        id: plus
        anchors.left: parent.left
        anchors.top: meterBody.top
        anchors.topMargin: 11
        source: "qrc:/imgs/toastmode/plus.png"

        MouseArea {
            anchors.fill: parent
            onClicked: incTemp()
        }
    }

    Image {
        id: minus
        anchors.left: parent.left
        anchors.top: plus.bottom
        anchors.topMargin: 335
        source: "qrc:/imgs/toastmode/minus.png"

        MouseArea {
            anchors.fill: parent
            onClicked: decTemp()
        }
    }

    Rectangle {
        width: 96
        height: 96
        color: "transparent"
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 285+50
        Image {
            id: slider
            source: "qrc:/imgs/toastmode/slider.png"
            MouseArea {
                anchors.fill: parent
                drag.target: slider
                drag.axis: Drag.YAxis
                drag.minimumY: -216
                drag.maximumY: 168

                onPressed: isIndicatorPressed=true
                onReleased: isIndicatorPressed=false
            }

            onYChanged: {

                if (!isIndicatorPressed) {
                    return
                }

                //                print("onYChanged, y: ", y)
                //                if (ys.length < 10) {
                //                    ys.push(-y)
                //                    return
                //                } else {
                //                    ys.shift()
                //                    ys.push(-y)
                //                }

                //                var ny = 0
                //                for (var k=0; k<10; ++k) {
                //                    ny += ys[k]
                //                }
                //                ny = parseInt(ny/10)

                //                var nInd = 22 + parseInt(ny/8)
                //                var nOffset = nInd - lmodel.count
                //                if (nOffset > 0) {
                //                    for (var i=0; i<nOffset; ++i) {
                //                        lmodel.append({ind:0})
                //                    }
                //                } else {
                //                    for (var ii=0; ii<(-1*nOffset); ++ii) {
                //                        lmodel.remove(lmodel.count-1)
                //                    }
                //                }
                //                var x = parseInt((lmodel.count-22)/8)
                //                displayNum = 220 + x*5
                ////                tNum.text = displayNum + "°c"
                //                tNum.source = "qrc:/imgs/themoCount/"+displayNum+".png"



                var yT = -y
                yT += 168

                var nBlockCount = parseInt(yT/8)
                lmodel.clear()
                for (var i=0; i<nBlockCount; ++i) {
                    lmodel.append({ind:0})
                }

                var xNum = parseInt(yT/15)
                print("xNum: ", xNum)
                if (xNum%5 === 0) {
                    displayNum = 210 + xNum
                    tNum.source = "qrc:/imgs/themoCount/"+displayNum+".png"
                }
            }
        }
    }



    Repeater {
        id: lvdots
        width: 30
        height: 336
        delegate: Image {
            source: "qrc:/imgs/toastmode/tempdots.png"
            anchors.horizontalCenter: meterBody.horizontalCenter
            anchors.bottom: meterBody.bottom
            anchors.bottomMargin: model.index*8+160
        }

        model: ListModel {
            id: lmodel
            ListElement {
                ind: 0
            }
            ListElement {
                ind: 1
            }
            ListElement {
                ind: 2
            }
            ListElement {
                ind: 3
            }
            ListElement {
                ind: 4
            }
            ListElement {
                ind: 5
            }
            ListElement {
                ind: 6
            }
            ListElement {
                ind: 7
            }
            ListElement {
                ind: 8
            }
            ListElement {
                ind: 9
            }
            ListElement {
                ind: 10
            }
            ListElement {
                ind: 11
            }
            ListElement {
                ind: 12
            }
            ListElement {
                ind: 13
            }
            ListElement {
                ind: 14
            }
            ListElement {
                ind: 15
            }
            ListElement {
                ind: 16
            }
            ListElement {
                ind: 17
            }
            ListElement {
                ind: 18
            }
            ListElement {
                ind: 19
            }
            ListElement {
                ind: 20
            }
            ListElement {
                ind: 21
            }
        }
    }


    function incTemp() {
        if (displayNum < 235) {
            displayNum += 5
            tNum.source = "qrc:/imgs/themoCount/"+displayNum+".png"

            //            var nInd = displayNum-210
            //            var nOffset = nInd - lmodel.count
            //            if (nOffset > 0) {
            //                for (var i=0; i<nOffset; ++i) {
            //                    lmodel.append({ind:0})
            //                }
            //            } else {
            //                for (var ii=0; ii<(-1*nOffset); ++ii) {
            //                    lmodel.remove(lmodel.count-1)
            //                }
            //            }

            var inds = [140,70,0,-70,-140,-210]

            var ix = (displayNum-210)/5
            var iy = inds[ix]



            var nInd = 22 + parseInt(-iy/8)
            var nOffset = nInd - lmodel.count
            if (nOffset > 0) {
                for (var i=0; i<nOffset; ++i) {
                    lmodel.append({ind:0})
                }
            } else {
                for (var ii=0; ii<(-1*nOffset); ++ii) {
                    lmodel.remove(lmodel.count-1)
                }
            }


            slider.y = iy

            print("inc y: ", slider.y, "  num: ", displayNum)

        }


    }

    function decTemp() {
        if (displayNum > 210) {
            displayNum -= 5
            tNum.source = "qrc:/imgs/themoCount/"+displayNum+".png"


            var inds = [140,70,0,-70,-140,-210]

            var ix = (displayNum-210)/5
            var iy = inds[ix]


            var nInd = 22 + parseInt(-iy/8)
            var nOffset = nInd - lmodel.count
            if (nOffset > 0) {
                for (var i=0; i<nOffset; ++i) {
                    lmodel.append({ind:0})
                }
            } else {
                for (var ii=0; ii<(-1*nOffset); ++ii) {
                    lmodel.remove(lmodel.count-1)
                }
            }


            slider.y = iy

            print("dec y: ", slider.y, "  num: ", displayNum)
        }
    }

}



